﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionCore
{
    public class Status_ExcTR : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = /*(ExcTR_Presence || ExcTR_Empty) && */Kernel.motion.ExcST_R.IsHomeAlready && Kernel.motion.ExcST_R.IsHomeAlready
                     && Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready
                     && Kernel.motion.SortST_X.IsHomeAlready && Kernel.motion.SortST_Y.IsHomeAlready && Kernel.motion.SortST_R.IsHomeAlready;
            start = condition && AutoMode;

            switch (ExcTRStatusCode)
            {
                case ExcTRStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            ExcTRStatusCode = ExcTRStatus.CheckStatus;
                        }
                    }
                    break;
                case ExcTRStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (/*ExcTR_Presence &&*/ Exc_Up && Exc_MS_Up)
                        {
                                                   }
                        else if (/*ExcTR_Empty && */Exc_Up && Exc_MS_Up)
                        {
                            if (ExcTR_R_LD && ExcTR_Z_Wait)
                            {
                                if (ExcTR_Open)
                                {
                                    ExcTRStatusCode = ExcTRStatus.X_ToLoad;
                                }
                                else
                                {
                                    ExcTRStatusCode = ExcTRStatus.LoadFrame;
                                }

                            }
                            else if (ExcTR_X_Exchange && ExcTR_R_LD && ExcTR_Open && ExcTR_Z_PreLD)
                            {
                                ExcTRStatusCode = ExcTRStatus.LoadFrameReq;
                            }
                            else if(ExcTR_X_Flip|| ExcTR_X_ULD||ExcST_Y_LD/*||ExcST_Empty*/)
                            {
                                ExcTRStatusCode = ExcTRStatus.Z_ToWait_1st;
                            }
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToWait_1st://XZ到翻转安全位
                    subStatusCode = "10: Z移动到安全位";
                    {
                        if (ExcTR_Z_Wait)
                        {
                            ExcTRStatusCode = ExcTRStatus.X_ToFlip;
                        }
                    }
                    break;
                case ExcTRStatus.X_ToFlip:
                    subStatusCode = "11: X移动到翻转位";
                    {
                        if (/*ExcTR_Presence && */ExcTR_Z_Wait && ExcTR_X_Flip)
                        {
                          
                        }
                        else if (/*ExcTR_Empty && */ExcTR_Z_Wait && ExcTR_X_Flip)
                        {
                            if (ExcTR_R_LD && ExcTR_Open)
                            {
                                ExcTRStatusCode = ExcTRStatus.X_ToLoad;

                            }
                            else
                            {
                                ExcTRStatusCode = ExcTRStatus.LoadFrame;
                            }
                        }
                    }
                    break;
                #region LoadFrame
                case ExcTRStatus.LoadFrame://铁圈上料
                    subStatusCode = "20: 进入铁圈上料";
                    {
                        if (true)
                        {
                            ExcTRStatusCode = ExcTRStatus.ClampCylinderOpen_1st;
                        }
                    }
                    break;
                case ExcTRStatus.ClampCylinderOpen_1st:
                    subStatusCode = "21: 夹爪气缸打开";
                    {
                        if (ExcTR_Open)
                        {
                            ExcTRStatusCode = ExcTRStatus.R_ToLoad;
                        }
                    }
                    break;
                case ExcTRStatus.R_ToLoad:
                    subStatusCode = "22: R移动到入料位";
                    {
                        if (ExcTR_R_LD)
                        {
                            ExcTR_FlipReq = false;
                            ExcTRStatusCode = ExcTRStatus.X_ToLoad;
                        }
                        else
                        {
                            ExcTR_FlipReq = true;
                        }
                    }
                    break;
                case ExcTRStatus.X_ToLoad:
                    subStatusCode = "23: X移动到入料位";
                    {
                        if (ExcTR_X_Exchange/* && ExcST_Presence*/ && ParameterHelper.ExcST.CST.Type == DeviceType.Frame)
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToPreLoad;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToPreLoad:
                    subStatusCode = "24: Z移动到预入料位";
                    {
                        if (ExcTR_Z_PreLD )
                        {
                            ExcTRStatusCode = ExcTRStatus.LoadFrameReq;
                        }
                    }
                    break;
                case ExcTRStatus.LoadFrameReq:
                    subStatusCode = "30: 铁圈取料请求";
                    {
                        if (ExcSTStatusCode == ExcSTStatus.UnLoadFrameReq && ExcST_Exchange)
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToLoad;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToLoad:
                    subStatusCode = "31: Z移动到入料位";
                    {
                        if (ExcTR_Z_LD)
                        {
                            ExcTRStatusCode = ExcTRStatus.ClampCylinderClose;
                        }
                    }
                    break;
                case ExcTRStatus.ClampCylinderClose:
                    subStatusCode = "32: 夹爪气缸闭合";
                    {
                        if (ExcTR_Close && !ExcST_Vac)
                        {
                            ParameterHelper.ExcTRDAL.Copy(ParameterHelper.FileName_ExcST);
                            ParameterHelper.ExcTR = ParameterHelper.ExcTRDAL.Load();
                            ParameterHelper.ExcST = new ChipInfo();
                            ProcedureHelper_Exc.SaveExcST();
                            //ExcTR_Presence = true;
                            ExcTRStatusCode = ExcTRStatus.Z_ToWait_2nd;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToWait_2nd:
                    subStatusCode = "33: Z移动到待命位";
                    {
                        if (ExcTR_Z_Wait)
                        {
                            ExcTRStatusCode = ExcTRStatus.LoadFrameComplete;
                        }
                    }
                    break;
                case ExcTRStatus.LoadFrameComplete:
                    subStatusCode = "34: 铁圈取料完成";
                    {
                        if (/*ExcTR_Presence &&*/ ExcSTStatusCode != ExcSTStatus.WaitingUnLoad)
                        {
                            ExcTRStatusCode = ExcTRStatus.CheckStatus;
                        }
                    }
                    break;
                #endregion

                #region 倒膜
                case ExcTRStatus.Exchange://倒膜
                    subStatusCode = "40: 进入倒膜";
                    {
                        if (ExcTR_X_Flip && ExcTR_Z_Wait)
                        {
                            ExcTRStatusCode = ExcTRStatus.R_ToExchange;
                        }
                    }
                    break;
                case ExcTRStatus.R_ToExchange:
                    subStatusCode = "41: R移动到倒膜位";
                    {
                        if (ExcTR_R_Exchange)
                        {
                            ExcTR_FlipReq = false; 
                            ExcTRStatusCode = ExcTRStatus.X_ToExchange;
                        }
                        else
                        {
                            ExcTR_FlipReq = true;
                        }
                    }
                    break;
                case ExcTRStatus.X_ToExchange:
                    subStatusCode = "42: X移动到倒膜位";
                    {
                        if (ExcTR_X_Exchange)
                        {
                            ExcTRStatusCode = ExcTRStatus.ExchangeReq;
                        }
                    }
                    break;
                case ExcTRStatus.ExchangeReq:
                    subStatusCode = "50: 倒膜请求";
                    {
                        if (ExcST_Exchange && ExcSTStatusCode == ExcSTStatus.ExchangeReq)
                        {
                            ExcTR_Exchanging = true;
                            ExcTRStatusCode = ExcTRStatus.Z_ToExchange;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToExchange:
                    subStatusCode = "51: Z移动到倒膜位";
                    {
                        if (ExcTR_Z_Exchange)
                        {
                            ExcTRStatusCode = ExcTRStatus.WaitingExchange;
                        }
                    }
                    break;
                case ExcTRStatus.WaitingExchange:
                    subStatusCode = "52: 等待倒膜饱压";
                    {
                       
                        if (ExchangeTimer.Duration >= 10000 && ExchangeTimer.Duration <= 12000)
                        {
                            Task.Run(() =>
                            {
                                Kernel.PresureSensor.SetCommand();
                            });
                        }
                        if (ExchangeTimer.Duration >= 30000 && ExchangeTimer.Duration <= 32000)
                        {
                            SaveExcTR();
                        }
                        if (ExcSTStatusCode == ExcSTStatus.ExchangeCylinder_ToUp && Exc_Up)
                        {
                            if (ParameterHelper.CurParam.Exc.SepratePeelingEnale)
                            {
                                ExcTRStatusCode = ExcTRStatus.Z_ToFirstPeeling;
                            }
                            else
                            {
                                WaitPeelingTimer.Start();
                                ExcTRStatusCode = ExcTRStatus.Z_ToExchange_Up;
                            }
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToFirstPeeling:
                    subStatusCode = "53: Z移动到一段分离位";
                    {
                        if (ExcTR_Z_FirstPeeling)
                        {
                            WaitPeelingTimer.Start();
                            ExcTRStatusCode = ExcTRStatus.Z_AtFirstPeelingWait;
                        }
                    }
                    break;
                case ExcTRStatus.Z_AtFirstPeelingWait:
                    subStatusCode = "54: Z移动到一段分离位等待";
                    {
                        if (WaitPeelingTimer.Duration >= ParameterHelper.CurParam.Exc.Peeling1WaitingTime * 1000)
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToExchange_2nd;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToExchange_2nd:
                    subStatusCode = "54: Z返回到倒膜位";
                    {
                        if (ExcTR_Z_Exchange)
                        {
                            WaitPeelingTimer.Start();
                            ExcTRStatusCode = ExcTRStatus.Z_ToExchange_Up;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToExchange_Up:
                    subStatusCode = "55: Z移动到拔模位";
                    {
                        if (ExcTR_Z_ExchangeUp)
                        {
                            ExcTR_Exchanging = false;
                            if (WaitPeelingTimer.Duration >= ParameterHelper.CurParam.Exc.PeelingWaitingTime * 1000)
                            {
                                ExcTRStatusCode = ExcTRStatus.Z_ToWait_3rd;
                            }
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToWait_3rd:
                    subStatusCode = "56: Z移动到等待位";
                    {
                        if (ExcTR_Z_Wait)
                        {

                                ExcTRStatusCode = ExcTRStatus.ExchangeComplete;
                        }
                    }
                    break;
                case ExcTRStatus.ExchangeComplete:
                    subStatusCode = "57: 倒膜完成";
                    {
                     
                    }
                    break;
                #endregion

                #region UnLoadFrame
                case ExcTRStatus.UnLoadFrame://出料给挑选平台
                    subStatusCode = "60: 进入出料";
                    {
                        if (ExcTR_X_Flip && ExcTR_Z_Wait)
                        {
                            ExcTRStatusCode = ExcTRStatus.R_ToUnLoad;
                        }
                    }
                    break;
                case ExcTRStatus.R_ToUnLoad:
                    subStatusCode = "61: R移动到出料位";
                    {
                        if (ExcTR_R_LD)
                        {
                            ExcTR_FlipReq = false;
                            ExcTRStatusCode = ExcTRStatus.X_ToUnLoad;
                        }
                        else
                        {
                            ExcTR_FlipReq = true;
                        }
                    }
                    break;
                case ExcTRStatus.X_ToUnLoad:
                    subStatusCode = "70: X移动到出料位";
                    {
                        if (ExcTR_X_ULD && SortSTStatusCode == SortSTStatus.LoadFromExcTRReq)
                        {
                            ExcTRStatusCode = ExcTRStatus.UnloadReq;
                        }
                    }
                    break;
                case ExcTRStatus.UnloadReq:
                    subStatusCode = "71: 出料请求";
                    {
                        if ( ExcTR_X_ULD && SortST_X_LD && SortST_Y_LD && SortST_R_Transfer && SortST_Down && SortST_NG_Down)
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToUnload;
                        }
                    }
                    break;

                case ExcTRStatus.Z_ToUnload:
                    subStatusCode = "72: Z移动到出料位";
                    {
                        if (ExcTR_Z_ULD && (SortST_Vac|| SortSTStatusCode == SortSTStatus.ST_VacOn_1st))
                        {
                            ExcTRStatusCode = ExcTRStatus.ClampCylinderOpen_2nd;
                        }
                    }
                    break;
                case ExcTRStatus.ClampCylinderOpen_2nd:
                    subStatusCode = "73: 夹爪打开";
                    {
                        if ( ExcTR_Open && (SortST_Vac || DryRunMode))
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToFinUnload;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToFinUnload:
                    subStatusCode = "74: Z移动到出料退避位";
                    {
                        if (SortSTStatusCode == SortSTStatus.LoadFromExcTRComplete && SortST_Y_ULD)
                        {
                            ExcTRStatusCode = ExcTRStatus.Z_ToWait_4th;
                        }
                    }
                    break;
                case ExcTRStatus.Z_ToWait_4th:
                    subStatusCode = "75: Z移动到待命位";
                    {
                        if (ExcTR_Z_Wait)
                        {
                            ExcTRStatusCode = ExcTRStatus.UnloadComplete;
                        }
                    }
                    break;
                case ExcTRStatus.UnloadComplete:
                    subStatusCode = "76: 出料完成";
                    {
                          ExcTRStatusCode = ExcTRStatus.Init;
                    }
                    break;
                #endregion
                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("倒膜移载- " + tempStatusCode);
            }
        }
    }
}
